<? include ("../init.php") ?>
<? include ("../includes/class.upload.php") ?>
<?
  $product_set_id = $_POST['product_set_id'];
  $name = addslashes($_POST['name']);
  $width = addslashes($_POST['width']);
  $depth = addslashes($_POST['depth']);
  $height = addslashes($_POST['height']);
  $material = addslashes($_POST['material']);
  $detail = addslashes($_POST['detail']);
  
  $sql = "INSERT INTO products VALUES(
    '',
    '$product_set_id',
    '$name',
    '$detail',
    '$width',
    '$depth',
    '$height',
    '$material',
    '',
    '1',
    '',
    '',
    '',
    '',
    '4'
    )";

  $db->execute($sql);
  
  $product_id = $db->lastInsertedId();
  
  if(isset($_FILES["img"]) && is_uploaded_file($_FILES["img"]["tmp_name"]) && $_FILES["img"]["tmp_name"]!=''){
    //$image = new image();
    //
    //ini_set("html_errors", "0");
    //
    //// Check the upload
    //if ($_FILES["img"]["error"] != 0) {
    //  echo "ERROR:invalid upload";
    //  exit(0);
    //}
    //
    //$name = get_file_name($product_id, $_FILES['img']['tmp_name']);
    //$path = "../../images/products/".$product_id;
    //if(!file_exists($path)){
    //  mkdir($path);
    //}
    //$urlpath = get_url_path($path);
    //$link = URL."/".$urlpath."/".$name;
    //$filename = $path ."/". $name;
    //$c_filename = $path ."/c_". $name;
    //
    //if (!move_uploaded_file($_FILES['img']['tmp_name'], $filename)){
    //  echo 'Problem: Could not move file to destination directory';
    //  exit(0);
    //}
    //
    //ini_set('memory_limit', '100M');
    //$image->load($filename);
    //$image->resize(278,188);
    //$image->save($filename);
    //
    //$image->load($c_filename);
    //$image->crop(274,121);
    //$image->save($c_filename);
    //ini_set('memory_limit', '16M');
    //
    //chmod($filename,0755);
    //
    //$sql = "UPDATE products SET product_img='$name' WHERE product_id=$product_id";
    //$db->execute($sql);
    
    $handle = new Upload($_FILES['img']);
    if ($handle->uploaded)
    {
      $handle->image_resize = true;
      $handle->image_ratio_fill = true;
      $handle->image_x = 274;
      $handle->image_y = 188;
      $handle->Process('../../images/products/'.$product_id);
      
      if ($handle->processed)
      {
        $sql = "UPDATE products SET product_img='".$handle->file_dst_name."' WHERE product_id=$product_id";
        $db->execute($sql);
        
        $handle->file_name_body_pre = 'c_';
        $handle->image_resize = true;
        $handle->image_ratio_fill = true;
        $handle->image_x = 274;
        $handle->image_y = 121;
        $handle->Process('../../images/products/'.$product_id);
        $handle->processed;
      }
      $handle->clean();
    }
  }
  
  if(isset($_FILES["img1"]) && is_uploaded_file($_FILES["img1"]["tmp_name"]) && $_FILES["img1"]["tmp_name"]!=''){
    //$image = new image();
    //
    //ini_set("html_errors", "0");
    //
    //// Check the upload
    //if ($_FILES["img1"]["error"] != 0) {
    //  echo "ERROR:invalid upload";
    //  exit(0);
    //}
    //
    //$name = get_file_name($product_id, $_FILES['img1']['tmp_name']);
    //$path = "../../images/products/".$product_id;
    //if(!file_exists($path)){
    //  mkdir($path);
    //}
    //$urlpath = get_url_path($path);
    //$link = URL."/".$urlpath."/".$name;
    //$filename = $path ."/". $name;
    //
    //if (!move_uploaded_file($_FILES['img1']['tmp_name'], $filename)){
    //  echo 'Problem: Could not move file to destination directory';
    //  exit(0);
    //}
    //
    //ini_set('memory_limit', '100M');
    //$image->load($filename);
    //$image->resize(143,60);
    //$image->save($filename);
    //ini_set('memory_limit', '16M');
    //
    //chmod($filename,0755);
    //
    //$sql = "UPDATE products SET product_img_2='$name' WHERE product_id=$product_id";
    //$db->execute($sql);
    
    $handle = new Upload($_FILES['img1']);
    if ($handle->uploaded)
    {
      $handle->image_resize = true;
      $handle->image_ratio_fill = true;
      $handle->image_x = 143;
      $handle->image_y = 60;
      $handle->Process('../../images/products/'.$product_id);
      
      if ($handle->processed)
      {
        $sql = "UPDATE products SET product_img_2='".$handle->file_dst_name."' WHERE product_id=$product_id";
        $db->execute($sql);
      }
      
      $handle->clean();
    }
  }
  
  if(isset($_FILES["img2"]) && is_uploaded_file($_FILES["img2"]["tmp_name"]) && $_FILES["img2"]["tmp_name"]!=''){
    //$image = new image();
    //
    //ini_set("html_errors", "0");
    //
    //// Check the upload
    //if ($_FILES["img2"]["error"] != 0) {
    //  echo "ERROR:invalid upload";
    //  exit(0);
    //}
    //
    //$name = get_file_name($product_id, $_FILES['img2']['tmp_name']);
    //$path = "../../images/products/".$product_id;
    //if(!file_exists($path)){
    //  mkdir($path);
    //}
    //$urlpath = get_url_path($path);
    //$link = URL."/".$urlpath."/".$name;
    //$filename = $path ."/". $name;
    //
    //if (!move_uploaded_file($_FILES['img2']['tmp_name'], $filename)){
    //  echo 'Problem: Could not move file to destination directory';
    //  exit(0);
    //}
    //
    //ini_set('memory_limit', '100M');
    //$image->load($filename);
    //$image->resize(143,60);
    //$image->save($filename);
    //ini_set('memory_limit', '16M');
    //
    //chmod($filename,0755);
    //
    //$sql = "UPDATE products SET product_img_3='$name' WHERE product_id=$product_id";
    //$db->execute($sql);
    
    $handle = new Upload($_FILES['img2']);
    if ($handle->uploaded)
    {
      $handle->image_resize = true;
      $handle->image_ratio_fill = true;
      $handle->image_x = 143;
      $handle->image_y = 60;
      $handle->Process('../../images/products/'.$product_id);
      
      if ($handle->processed)
      {
        $sql = "UPDATE products SET product_img_3='".$handle->file_dst_name."' WHERE product_id=$product_id";
        $db->execute($sql);
      }
      $handle->clean();
    }
  }
  
  if(isset($_FILES["img3"]) && is_uploaded_file($_FILES["img3"]["tmp_name"]) && $_FILES["img3"]["tmp_name"]!=''){
    //$image = new image();
    //
    //ini_set("html_errors", "0");
    //
    //// Check the upload
    //if ($_FILES["img3"]["error"] != 0) {
    //  echo "ERROR:invalid upload";
    //  exit(0);
    //}
    //
    //$name = get_file_name($product_id, $_FILES['img3']['tmp_name']);
    //$path = "../../images/products/".$product_id;
    //if(!file_exists($path)){
    //  mkdir($path);
    //}
    //$urlpath = get_url_path($path);
    //$link = URL."/".$urlpath."/".$name;
    //$filename = $path ."/". $name;
    //
    //if (!move_uploaded_file($_FILES['img3']['tmp_name'], $filename)){
    //  echo 'Problem: Could not move file to destination directory';
    //  exit(0);
    //}
    //
    //ini_set('memory_limit', '100M');
    //$image->load($filename);
    //$image->resize(143,60);
    //$image->save($filename);
    //ini_set('memory_limit', '16M');
    //
    //chmod($filename,0755);
    //
    //$sql = "UPDATE products SET product_img_4='$name' WHERE product_id=$product_id";
    //$db->execute($sql);
    
    $handle = new Upload($_FILES['img3']);
    if ($handle->uploaded)
    {
      $handle->image_resize = true;
      $handle->image_ratio_fill = true;
      $handle->image_x = 143;
      $handle->image_y = 60;
      $handle->Process('../../images/products/'.$product_id);
      
      if ($handle->processed)
      {
        $sql = "UPDATE products SET product_img_4='".$handle->file_dst_name."' WHERE product_id=$product_id";
        $db->execute($sql);
      }
      $handle->clean();
    }
  }
  
  header("Location:product_list.php?aid=".$product_set_id);
?>